﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using tsInput.libs;

namespace tsInput
{
    public partial class Form1 : Form
    {
        TInput _inputBox = null;
        TMqClient mq = new TMqClient();
        public TInput FeInputBox
        {
            get
            {
                if (_inputBox == null)
                {
                    _inputBox = new TInput01();
                    _inputBox.onReady = onReady;
                    _inputBox.onDataChange = onDataChange;
                    _inputBox.onTimeChange = onTimeChange;
                }
                return _inputBox;
            }
        }
        

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //pg.SelectedObject = FeInputBox;
            mq.connect();

        }

        void log(String logData) {
            String d = DateTime.Now.ToString()+"\t"+ logData+"\r\n";
            Console.WriteLine(d);
            //txtLog.Text =   txtLog.Text + d;
        }

        void onReady(object sender, EventArgs e)
        { 
            log("ready!");
            String d = FeInputBox.ToJson();
            log(d);
        }
        void onDataChange(object sender, String dataKey)
        {
            log("dataChange!");
            String d = FeInputBox.ToJson();
            sendStatus();
            log(d);

        }
        void onTimeChange(object sender, String currentTime)
        {
            log("timeChange!");
            String d = FeInputBox.CurrentTime;
            sendTm();
            log(d);

        }
        void sendStatus( )
        {
            Dictionary<String, object> info = this.FeInputBox.getDict();
            info.Add("action", "data");
            String content = Newtonsoft.Json.JsonConvert.SerializeObject(info);
            String topic = mq.notifyTopic + "/ts/data/" + FeInputBox.Code;
            mq.client.Publish(topic, Encoding.ASCII.GetBytes(content));
            Console.Write("send notify:\t" + topic + "\t" + content);
        }
        void sendTm( )
        {
            Dictionary<String, object> info = new Dictionary<string, object>();
            info.Add("ct", FeInputBox.CurrentTime);
            info.Add("action", "currentTime");
            info.Add("Code", FeInputBox.Code);
            String content = Newtonsoft.Json.JsonConvert.SerializeObject(info);
            String topic = mq.notifyTopic + "/ts/time/" + FeInputBox.Code;
            mq.client.Publish(topic, Encoding.ASCII.GetBytes(content));
            Console.Write("send notify:\t" + topic + "\t" + content);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            FeInputBox.open();
            mq.connect();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            FeInputBox.close();
        }

        private void send_Click(object sender, EventArgs e)
        {

        }

        private void btnSendData_Click(object sender, EventArgs e)
        {
            sendStatus();
        }

        private void btnSendTime_Click(object sender, EventArgs e)
        {
            sendTm();
        }
    }
}
